﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Reflection;

namespace AAA.Base.Util
{
    public class WordConverter
    {
        private Application _objWord;

        public WordConverter()
        {
            try
            {
                _objWord = new Microsoft.Office.Interop.Word.Application();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

        }

        public void Release()
        {
            object sc = 0;
            object of = Missing.Value;
            object rd = Missing.Value;
            _objWord.Quit(ref sc, ref of, ref rd);
            _objWord = null;
        }

        private void Close(Document document)
        {
            try
            {
                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
                object routeDocument = true;
                document.Close(ref saveChanges, ref originalFormat, ref routeDocument);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        public string TC2SC(string strSource)
        {
            string strResult;            
            object t = Missing.Value;
            object nt = Missing.Value;
            object dt = Missing.Value;
            object v = true;
            Document objDocument = _objWord.Documents.Add(ref t, ref nt, ref dt, ref v);
            _objWord.Selection.TypeText(strSource);
            _objWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true);
            _objWord.ActiveDocument.Select();
            strResult = _objWord.Selection.Text;
            Close(objDocument);
            
//            object sc = 0;
//            object of = Missing.Value;
//            object rd = Missing.Value;
//            _objWord.Quit(ref sc, ref of, ref rd);            
            objDocument = null;
            //_objWord = null;
            return strResult;
        }
        public string SC2TC(string strSource)
        {
            string strResult;
            Application _objWord = new Microsoft.Office.Interop.Word.Application();
            object t = Missing.Value;
            object nt = Missing.Value;
            object dt = Missing.Value;
            object v = true;
            Document objDocument = _objWord.Documents.Add(ref t, ref nt, ref dt, ref v);
            _objWord.Selection.TypeText(strSource);
            _objWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
            _objWord.ActiveDocument.Select();
            strResult = _objWord.Selection.Text;
//            object sc = 0;
//            object of = Missing.Value;
//            object rd = Missing.Value;
//            _objWord.Quit(ref sc, ref of, ref rd);
            Close(objDocument);
            objDocument = null;
            //_objWord = null;
            return strResult;
        }

    }
}
